<template>
  <div class="recharge-management">
    <h2 style="margin-bottom: 20px;">充值记录管理</h2>
    
    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="订单编号">
          <el-input
            v-model="searchForm.search"
            placeholder="请输入订单编号或用户昵称"
            clearable
            style="width: 200px;"
          />
        </el-form-item>
        
        <el-form-item label="支付状态">
          <el-select
            v-model="searchForm.payment_status"
            placeholder="请选择支付状态"
            clearable
            style="width: 120px;"
          >
            <el-option label="待支付" value="pending" />
            <el-option label="已支付" value="paid" />
            <el-option label="支付失败" value="failed" />
            <el-option label="已退款" value="refunded" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="getRecharges">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 统计信息 -->
    <el-row :gutter="16" style="margin-bottom: 20px;">
      <el-col :xs="12" :sm="6">
        <div class="stat-card">
          <div class="stat-title">今日充值</div>
          <div class="stat-value">￥{{ statistics.today_amount }}</div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="6">
        <div class="stat-card">
          <div class="stat-title">总充值金额</div>
          <div class="stat-value">￥{{ statistics.total_amount }}</div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="6">
        <div class="stat-card">
          <div class="stat-title">充值次数</div>
          <div class="stat-value">{{ statistics.paid_recharges }}</div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="6">
        <div class="stat-card">
          <div class="stat-title">成功率</div>
          <div class="stat-value">
            {{ statistics.total_recharges > 0 ? ((statistics.paid_recharges / statistics.total_recharges) * 100).toFixed(1) : 0 }}%
          </div>
        </div>
      </el-col>
    </el-row>
    
    <!-- 充值记录表格 -->
    <div class="content-card">
      <el-table
        v-loading="loading"
        :data="recharges"
        style="width: 100%"
      >
        <el-table-column prop="recharge_no" label="充值单号" width="180" />
        
        <el-table-column label="用户信息" min-width="150">
          <template #default="scope">
            <div v-if="scope.row.user">
              <div class="user-name">{{ scope.row.user.nickname || '未设置' }}</div>
              <div class="user-phone">{{ scope.row.user.phone || '未绑定' }}</div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="充值信息" min-width="150">
          <template #default="scope">
            <div class="recharge-info">
              <div class="package-type">{{ scope.row.package_type }}</div>
              <div class="amount-info">
                充值：￥{{ scope.row.amount }}
                <span v-if="scope.row.bonus_amount > 0" class="bonus">
                  ＋￥{{ scope.row.bonus_amount }}
                </span>
              </div>
              <div class="total-amount">到账：￥{{ scope.row.total_amount }}</div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="支付方式" width="100">
          <template #default="scope">
            <el-tag :type="getPaymentMethodType(scope.row.payment_method)">
              {{ getPaymentMethodText(scope.row.payment_method) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="支付状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.payment_status)">
              {{ getStatusText(scope.row.payment_status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="支付时间" width="160">
          <template #default="scope">
            {{ scope.row.paid_at ? formatDate(scope.row.paid_at) : '-' }}
          </template>
        </el-table-column>
        
        <el-table-column label="创建时间" width="160">
          <template #default="scope">
            {{ formatDate(scope.row.created_at) }}
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="viewRecharge(scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.per_page"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="getRecharges"
          @current-change="getRecharges"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import axios from '@/utils/request'

const loading = ref(false)

// 搜索表单
const searchForm = ref({
  search: '',
  payment_status: ''
})

const dateRange = ref([])

// 充值记录列表
const recharges = ref([])

// 统计数据
const statistics = ref({
  total_recharges: 0,
  paid_recharges: 0,
  total_amount: 0,
  today_amount: 0
})

// 分页
const pagination = ref({
  page: 1,
  per_page: 20,
  total: 0
})

// 获取充值记录
const getRecharges = async () => {
  loading.value = true
  try {
    const params = {
      page: pagination.value.page,
      per_page: pagination.value.per_page,
      search: searchForm.value.search,
      payment_status: searchForm.value.payment_status
    }
    
    if (dateRange.value && dateRange.value.length === 2) {
      params.start_date = dateRange.value[0]
      params.end_date = dateRange.value[1]
    }
    
    const response = await axios.get('/api/recharges', { params })
    
    if (response.success) {
      recharges.value = response.data.recharges
      pagination.value = response.data.pagination
    }
  } catch (error) {
    console.error('获取充值记录失败:', error)
    ElMessage.error('获取充值记录失败')
  } finally {
    loading.value = false
  }
}

// 获取统计数据
const getStatistics = async () => {
  try {
    const response = await axios.get('/api/recharges/statistics')
    if (response.success) {
      statistics.value = response.data
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    search: '',
    payment_status: ''
  }
  dateRange.value = []
  getRecharges()
}

// 查看充值记录
const viewRecharge = (recharge) => {
  ElMessage.info('充值记录详情功能开发中')
}

// 获取支付方式文本
const getPaymentMethodText = (method) => {
  switch (method) {
    case 'wechat': return '微信支付'
    case 'alipay': return '支付宝'
    case 'balance': return '余额支付'
    default: return method
  }
}

// 获取支付方式标签类型
const getPaymentMethodType = (method) => {
  switch (method) {
    case 'wechat': return 'success'
    case 'alipay': return 'primary'
    case 'balance': return 'warning'
    default: return 'info'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'pending': return '待支付'
    case 'paid': return '已支付'
    case 'failed': return '支付失败'
    case 'refunded': return '已退款'
    default: return status
  }
}

// 获取状态标签类型
const getStatusType = (status) => {
  switch (status) {
    case 'pending': return 'warning'
    case 'paid': return 'success'
    case 'failed': return 'danger'
    case 'refunded': return 'info'
    default: return 'info'
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '-'
  return new Date(dateString).toLocaleString('zh-CN')
}

onMounted(() => {
  getRecharges()
  getStatistics()
})
</script>

<style lang="scss" scoped>
.stat-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  margin-bottom: 16px;
}

.stat-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.user-name {
  font-weight: 500;
  margin-bottom: 4px;
}

.user-phone {
  font-size: 12px;
  color: #666;
}

.recharge-info {
  font-size: 14px;
}

.package-type {
  font-weight: 500;
  margin-bottom: 4px;
}

.amount-info {
  margin-bottom: 4px;
  
  .bonus {
    color: #1890ff;
    font-weight: 500;
  }
}

.total-amount {
  color: #52c41a;
  font-weight: 600;
}
</style>